/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kungkhies.desktop.kungkhie.node.viewer;

import com.kungkhies.desktop.library.entities.Ilo;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.SwingUtilities;
import org.openide.util.LookupEvent;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Lookup;
import org.openide.util.LookupListener;
import org.openide.util.RequestProcessor;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
import org.openide.windows.WindowManager;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//com.kungkhies.desktop.kungkhie.node.viewer//KungkhieNodeViewer//EN",
autostore = false)
@TopComponent.Description(preferredID = "KungkhieNodeViewerTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "com.kungkhies.desktop.kungkhie.node.viewer.KungkhieNodeViewerTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_KungkhieNodeViewerAction",
preferredID = "KungkhieNodeViewerTopComponent")
public final class KungkhieNodeViewerTopComponent extends TopComponent implements LookupListener
{
    private com.kungkhies.desktop.library.entities.Node kungkhieNode;
    private final InstanceContent content;
    private Lookup.Result result = null;

    public KungkhieNodeViewerTopComponent()
    {
        initComponents();
        setName(NbBundle.getMessage(KungkhieNodeViewerTopComponent.class, "CTL_KungkhieNodeViewerTopComponent"));
        setToolTipText(NbBundle.getMessage(KungkhieNodeViewerTopComponent.class, "HINT_KungkhieNodeViewerTopComponent"));

         //Create a new instance of our dynamic object:
        content = new InstanceContent();

        //Add the dynamic object to the TopComponent Lookup:
        associateLookup(new AbstractLookup(content));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        displayNameLabel = new javax.swing.JLabel();
        displayNameTextLabel = new javax.swing.JLabel();
        nameTextLabel = new javax.swing.JLabel();
        typeLabel = new javax.swing.JLabel();
        typeTextLabel = new javax.swing.JLabel();
        descriptionLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionTextArea = new javax.swing.JTextArea();
        ilosLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        ilosTextArea = new javax.swing.JTextArea();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(KungkhieNodeViewerTopComponent.class, "KungkhieNodeViewerTopComponent.jLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(nameLabel, org.openide.util.NbBundle.getMessage(KungkhieNodeViewerTopComponent.class, "KungkhieNodeViewerTopComponent.nameLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(displayNameLabel, org.openide.util.NbBundle.getMessage(KungkhieNodeViewerTopComponent.class, "KungkhieNodeViewerTopComponent.displayNameLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(displayNameTextLabel, org.openide.util.NbBundle.getMessage(KungkhieNodeViewerTopComponent.class, "KungkhieNodeViewerTopComponent.displayNameTextLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(nameTextLabel, org.openide.util.NbBundle.getMessage(KungkhieNodeViewerTopComponent.class, "KungkhieNodeViewerTopComponent.nameTextLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(typeLabel, org.openide.util.NbBundle.getMessage(KungkhieNodeViewerTopComponent.class, "KungkhieNodeViewerTopComponent.typeLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(typeTextLabel, org.openide.util.NbBundle.getMessage(KungkhieNodeViewerTopComponent.class, "KungkhieNodeViewerTopComponent.typeTextLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(descriptionLabel, org.openide.util.NbBundle.getMessage(KungkhieNodeViewerTopComponent.class, "KungkhieNodeViewerTopComponent.descriptionLabel.text")); // NOI18N

        descriptionTextArea.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        descriptionTextArea.setColumns(20);
        descriptionTextArea.setRows(5);
        descriptionTextArea.setFocusable(false);
        jScrollPane1.setViewportView(descriptionTextArea);

        org.openide.awt.Mnemonics.setLocalizedText(ilosLabel, org.openide.util.NbBundle.getMessage(KungkhieNodeViewerTopComponent.class, "KungkhieNodeViewerTopComponent.ilosLabel.text")); // NOI18N

        ilosTextArea.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        ilosTextArea.setColumns(20);
        ilosTextArea.setRows(5);
        ilosTextArea.setFocusable(false);
        jScrollPane2.setViewportView(ilosTextArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(ilosLabel)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE)
                        .addComponent(descriptionLabel)
                        .addComponent(displayNameTextLabel)
                        .addComponent(displayNameLabel)
                        .addComponent(typeTextLabel)
                        .addComponent(typeLabel)
                        .addComponent(nameTextLabel)
                        .addComponent(nameLabel)
                        .addComponent(jLabel1)))
                .addContainerGap(107, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(jLabel1)
                .addGap(48, 48, 48)
                .addComponent(nameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(nameTextLabel)
                .addGap(33, 33, 33)
                .addComponent(typeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(typeTextLabel)
                .addGap(29, 29, 29)
                .addComponent(displayNameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(displayNameTextLabel)
                .addGap(34, 34, 34)
                .addComponent(descriptionLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addComponent(ilosLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(116, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JTextArea descriptionTextArea;
    private javax.swing.JLabel displayNameLabel;
    private javax.swing.JLabel displayNameTextLabel;
    private javax.swing.JLabel ilosLabel;
    private javax.swing.JTextArea ilosTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel nameTextLabel;
    private javax.swing.JLabel typeLabel;
    private javax.swing.JLabel typeTextLabel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened()
    {
        RequestProcessor.getDefault().post(new Runnable()
        {
            @Override
            public void run()
            {
                readKungkhieNode();
            }
        });  
    }

    @Override
    public void componentClosed()
    {
        result.removeLookupListener(this);
        result = null; 
    }
    
    private void readKungkhieNode()
    {
        //JavaDBSupport.ensureStartedDB();
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                TopComponent tc = WindowManager.getDefault().findTopComponent("KungkhieNodeExplorerTopComponent");
                if (tc == null)
                {
                    System.out.println("Kungkhie Node Viewer. readNode: top component in get default is null");
                    return;
                }
                result = tc.getLookup().lookupResult(com.kungkhies.desktop.library.entities.Node.class);
                result.addLookupListener(KungkhieNodeViewerTopComponent.this);
                resultChanged(new LookupEvent(result));
            }
        });
    }

    void writeProperties(java.util.Properties p)
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p)
    {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public void resultChanged(LookupEvent le)
    {
        Lookup.Result r = (Lookup.Result) le.getSource();
        Collection<com.kungkhies.desktop.library.entities.Node> coll = (Collection<com.kungkhies.desktop.library.entities.Node>) r.allInstances();
        if (!coll.isEmpty())
        {
            for (com.kungkhies.desktop.library.entities.Node nod : coll)
            {
                kungkhieNode = nod;
                nameTextLabel.setText(nod.getName());
                typeTextLabel.setText(nod.getNodeType());
                displayNameTextLabel.setText(nod.getDisplayName());
                descriptionTextArea.setText(nod.getDescription());
                Collection<Ilo> iloList = nod.getIloCollection();
                Iterator<Ilo> iloIt = iloList.iterator();
                
                while (iloIt.hasNext())
                {
                   ilosTextArea.append(iloIt.next().getIloText());
                }
            }
        }
        else
        {
                nameTextLabel.setText("[no selection]");
                displayNameTextLabel.setText("");
                typeTextLabel.setText("");
                descriptionTextArea.setText("");
                ilosTextArea.setText("");
        }
    }
}
